﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Htinns.GatewayConsole.Models;
using Htinns.GatewayConsole.Extentions;
using System.Data;

namespace Htinns.GatewayConsole.Controllers
{
	public class HomeController : Controller
	{
		public ActionResult Index(string id)
		{
			//获取酒店
			Session["hotelId"] = id;

			//添加该酒店的公用投票

			var modules = ModuleService.GetModules(id);

			if (!string.IsNullOrEmpty(id)) {
				modules=modules.Union(ModuleService.GetModules(ConstTypes.Questionnaire_PublicId));
			}

			ViewBag.LeftModules = modules.Where(m => m.Position == ConstTypes.ModulePosition_Zero);

			ViewBag.MiddleModules = modules.Where(m => m.Position == ConstTypes.ModulePosition_One);

			ViewBag.SecondModules = modules.Where(m => m.Position == ConstTypes.ModulePosition_Two);

			ViewBag.Error = TempData["error"];			

			return View();
		}

		[HttpGet]
		public ActionResult Questionnaire(string id) 
		{
			if(!string.IsNullOrEmpty(id)){
				Session["hotelId"] = id;
			}

			var hotelId = (string)Session["hotelId"];

			if (string.IsNullOrEmpty(hotelId)) return RedirectToAction("Index");

			ViewBag.HotelId = hotelId;

			Vote vote = ModuleService.GetPublicVote(hotelId);

			return View(vote);
		}

		[HttpPost]
		[Resubmit]
		public ActionResult Questionnaire(string id,FormCollection form)
		{
			if (!string.IsNullOrEmpty(id))
			{
				Session["hotelId"] = id;
			}

			var hotelId = (string)Session["hotelId"];

			if (string.IsNullOrEmpty(hotelId)) return RedirectToAction("Index");
			
			ViewBag.HotelId = hotelId;	

			try {
				
				SubmitVoteSheet(hotelId,form);
				
			}catch(Exception error){

				Vote vote = ModuleService.GetPublicVote(hotelId);

				return View(vote);
			}	

			return RedirectToAction("ProcessSuccess", new { id = hotelId});			
		}		

		[HttpGet]
		public ActionResult QuestionnaireResult(string id) 
		{
			if (!string.IsNullOrEmpty(id))
			{
				Session["hotelId"] = id;
			}

			var hotelId = (string)Session["hotelId"];

			if (string.IsNullOrEmpty(hotelId)) return RedirectToAction("Index");		

			ViewBag.HotelId = hotelId;			

			Vote vote = ModuleService.GetPublicVote(hotelId);

			ViewBag.VoteSheetCount = ModuleService.GetVoteSheetCount(vote, hotelId);

			ViewBag.VoteMsg = ModuleService.GetSheetMsg(vote,hotelId,DateTime.Today.AddDays(-2),DateTime.Today);

			return View(vote);
		}

		[HttpGet]
		public ActionResult GetVoteSheetMsg(string id,DateTime beginDate,DateTime endDate)
		{
			string jsonFormat = "'i':'{0}','n':'{1}','d':'{2}','t':'{3}','r':'{4}'";

			Vote vote = ModuleService.GetPublicVote(id);

			var sheets = ModuleService.GetSheetMsg(vote, id, beginDate, endDate);

			if (sheets.Count() <= 0) return Content(string.Empty);

			IList<string> list=new List<string>();

			foreach (var sheet in sheets) { 
				list.Add("{"+string.Format(jsonFormat,sheet.Id,sheet.Operator,sheet.Description,sheet.CreatedDate.ToString(),sheet.RoomNo)+"}");
			}

			return Content("["+string.Join(",",list.ToArray())+"]");
			
		}

		[HttpGet]
		public ActionResult ProcessSuccess(string id) 
		{
			ViewBag.HotelId = id;

			if (string.IsNullOrEmpty(id)) return RedirectToAction("Index");

			return View();
		}

		[HttpGet]
		public ActionResult ShowModule(int id,int type) 
		{
			//根据ID获取模块
			//根据模块获取具体类型
			using (GatewayContext db = new GatewayContext())
			{			
				if (type == ConstTypes.ModuleType_Atricle)
				{
					var article = db.Atricles.Find(id);
					return PartialView("Controls/ModuleAd", article);
				}
				else {
					var vote = db.Votes.Find(id);
					return PartialView("Controls/ModuleVote", vote);
				}
				
				
			}
			
		}

		[HttpGet]
		public ActionResult Resubmit() 
		{
			ViewBag.HotelId = Session["hotelId"] ;

			if (string.IsNullOrEmpty(ViewBag.HotelId)) return RedirectToAction("Index");

			return View();
		
		}

		[HttpGet]
		public ActionResult About(string callback)
		{		
			return View();
		}		

		private void SubmitVoteSheet(string hotelId,FormCollection form)
		{
			IList<VoteItem> items = new List<VoteItem>();		

			using (GatewayContext db = new GatewayContext())
			{	
				if (form["v3"] == "3")
				{
					items.Add(db.VoteItems.Find(3));
				}
				if (form["v4"] == "4")
				{
					items.Add(db.VoteItems.Find(4));
				}
				if (form["v5"] == "5")
				{
					items.Add(db.VoteItems.Find(5));
				}
				if (form["v6"] == "6")
				{
					items.Add(db.VoteItems.Find(6));
				}
				if (form["v7"] == "7")
				{
					items.Add(db.VoteItems.Find(7));
				}
				if (form["v8"] == "8")
				{
					items.Add(db.VoteItems.Find(8));
				}
				if (form["v9"] == "9")
				{
					items.Add(db.VoteItems.Find(9));
				}
				if (form["v10"] == "10")
				{
					items.Add(db.VoteItems.Find(10));
				}
				if (form["net"] == "1")
				{
					items.Add(db.VoteItems.Find(1));
				}
				else {
					items.Add(db.VoteItems.Find(2));
				}
				VoteSheet sheet = new VoteSheet() { 
					HotelId=hotelId,
					Vote=db.Votes.Find(1),
					VoteItems=items,					
					RoomNo=form["RoomNo"].Trim(),
					Description=form["reason"].Trim(),
					Creater=form["Name"].Trim(),
					Operator = form["Name"].Trim(),
					ModifiedDate=DateTime.Now,
					CreatedDate=DateTime.Now
				};

				db.Entry(sheet).State = EntityState.Added;
				db.SaveChanges();
			}
		}
	}
}
